<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/2  17:49
 */
namespace app\admin\Controller;
use think\Db;

class Member extends Common
{
    /*
   *	构造函数
   */
    function __construct(){
        parent::__construct();
    }

    //会员列表
    public function index()
    {
//        $data = request()->param();
//        var_dump($data);
//        die(123);
        $pageParam = ['query' =>[]];
        $where = [];
        $status =input("status");
        if($status!=''){
            $where['status']=$status;
            $pageParam['query']['status'] = $status;
        }else{
            $where['status'] = 1;
        }
        $this->assign('status', $status);
        $email =input("email");
        if($email!=''){
            $where['email']=$email;
            $pageParam['query']['email'] = $email;
        }
        $this->assign('email', $email);

        $is_buyer=input("is_buyer");
        if($is_buyer!=''){
            $where['is_buyer']=$is_buyer;
            $pageParam['query']['is_buyer'] = $is_buyer;
        }
        $this->assign('is_buyer', $is_buyer);

        $keyword = input("keyword");
        if($keyword != ''){
            $where['nickname|mobile']=array("like",'%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
        }
        $this->assign('keyword', $keyword);
//        if(input("export")){
//            $this->export($where);exit;
//        }

        $list = db('member')->where($where)->order('id desc')->paginate(15, false, $pageParam);
        $page = $list->render();
        $this->assign('page',$page);
        $this->assign('users',$list);
        return $this->fetch();
    }

    //会员修改
    public function edit()
    {
        $id = input('id/d');
        if(request()->isAjax()){
            $id = input('id');
            $nickname = input('nickname');
            $mobile = input('mobile');
            $email = input('email');
            $province = input('province');
            $city = input('city');
            $county = input('county');
            $address = input('address');
            $is_buyer = input('is_buyer');
            $status = input('status');
            $sex = input('sex');
            if($sex){
                $data['sex'] = $sex;
            }
            $data =[
                'nickname'=>$nickname,
                'mobile'=>$mobile,
                'email'=>$email,
                'province'=>$province,
                'city'=>$city,
                'county'=>$county,
                'address'=>$address,
                'is_buyer'=>$is_buyer,
                'status'=>$status
            ];

            $res = db('member')->where('id='.$id)->update($data);
            if($res){
                $this->success('操作成功！',url('Member/index'));
            }else{
                $this->error('操作失败！');
            }
        }
        $da = db('member')->where('id='.$id)->find();
        $this->assign('da', $da);
        return $this->fetch();
    }

    //买手申请列表
    public function buyer()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $status =input("status");
        if($status!=''){
            $where['status']=$status;
            $pageParam['query']['status'] = $status;
        }
        $this->assign('status', $status);

        $name =input("name");
        if($name!=''){
            $where['name']=$name;
            $pageParam['query']['name'] = $name;
            $this->assign('name', $name);
        }

        $mobile =input("mobile");
        if($mobile!=''){
            $where['mobile']=$mobile;
            $pageParam['query']['mobile'] = $mobile;
            $this->assign('mobile', $mobile);
        }

        $id_card =input("id_card");
        if($id_card!=''){
            $where['id_card']=$id_card;
            $pageParam['query']['id_card'] = $id_card;
            $this->assign('id_card', $id_card);
        }

        $data = db('member_buyer_check')->where($where)->order('id desc')->paginate(15,false,$pageParam);
        $this->assign('page',$data->render());
        $this->assign('da', $data);
        return $this->fetch();
    }

    //审核
    public function checkbuyer()
    {
        if(request()->isAjax()){
            $id = input('id/d');
            $uid = input('uid');
            $status = input('status');

            $data = [
                'status'=>$status,
                'check_time'=>time(),
            ];

            $res = db('member_buyer_check')->where('id='.$id)->update($data);

            if($status == '1'){
                $member['is_buyer']=1;
            }

            if($status == '-1'){
                $member['is_buyer']=0;
            }
            
            $resm = db('member')->where('id='.$uid)->update($member);
            if($res && $resm){
                $this->success('操作成功！',url('Member/buyer'));
            }else{
                $this->error('操作失败！');
            }
        }
        $id = input('id/d');
        $da = db('member_buyer_check')->where('id='.$id)->find();
        $this->assign('da',$da);
        return $this->fetch();
    }

    //删除会员
//    public function del()
//    {
//        $id = input('id/d');
//        if(!$id)return $this->error('参数错误');
//        $data['status'] = -1;
//        $res = db('member_buyer_check')->where('id='.$id)->update($data);
//        if(!$res)return $this->error('操作失败！');
//       return $this->success('操作成功！', url('Member/buyer'));
//
//    }

//    删除买手
    public function delbuyer()
    {
        $id = input('id/d');
        if(!$id)return $this->error('参数错误');
        $res = db('member_buyer_check')->where('id='.$id)->delete();
        if(!$res)return $this->error('操作失败！');
        return $this->success('操作成功！', url('Member/buyer'));
    }
}